import {defineStore} from 'pinia'
import {spaceApi} from '@/api'
import type {MySpaceVO, SpaceUserListVO, SpaceVO} from '@/types/vo/space'

interface SpaceState {
    currentSpace: SpaceVO | null
    mySpaces: MySpaceVO | null
    spaceMembers: SpaceUserListVO | null
    loading: boolean
}

export const useSpaceStore = defineStore('space', {
    state: (): SpaceState => ({
        currentSpace: null,
        mySpaces: null,
        spaceMembers: null,
        loading: false,
    }),

    getters: {
        hasSpaces: (state) => {
            return (state.mySpaces?.owner?.length || 0) + (state.mySpaces?.joined?.length || 0) > 0
        },
        isOwner: (state) => {
            return state.currentSpace?.currentUserRole === 'owner'
        },
        isEditor: (state) => {
            const role = state.currentSpace?.currentUserRole
            return role === 'owner' || role === 'editor'
        },
        canManageMembers: (state) => {
            return state.currentSpace?.currentUserRole === 'owner'
        },
    },

    actions: {
        /**
         * 创建空间
         */
        async createSpace(payload: any) {
            try {
                this.loading = true
                const space = await spaceApi.create(payload)
                // 重新获取我的空间列表
                await this.fetchMySpaces()
                return space
            } finally {
                this.loading = false
            }
        },

        /**
         * 获取我的空间列表
         */
        async fetchMySpaces() {
            try {
                this.loading = true
                const mySpaces = await spaceApi.my()
                this.mySpaces = mySpaces
                return mySpaces
            } finally {
                this.loading = false
            }
        },

        /**
         * 获取空间详情
         */
        async fetchSpaceById(id: number) {
            try {
                this.loading = true
                const space = await spaceApi.getById(id)
                this.currentSpace = space
                return space
            } finally {
                this.loading = false
            }
        },

        /**
         * 更新空间信息
         */
        async updateSpace(id: number, payload: any) {
            try {
                this.loading = true
                const space = await spaceApi.update(id, payload)
                this.currentSpace = space
                return space
            } finally {
                this.loading = false
            }
        },

        /**
         * 升级空间
         */
        async upgradeSpace(id: number, payload: any) {
            try {
                this.loading = true
                const space = await spaceApi.upgrade(id, payload)
                this.currentSpace = space
                return space
            } finally {
                this.loading = false
            }
        },

        /**
         * 生成邀请码
         */
        async generateInviteCode(id: number) {
            return await spaceApi.generateInviteCode(id)
        },

        /**
         * 加入空间
         */
        async joinSpace(payload: any) {
            try {
                this.loading = true
                const space = await spaceApi.join(payload)
                // 重新获取我的空间列表
                await this.fetchMySpaces()
                return space
            } finally {
                this.loading = false
            }
        },

        /**
         * 获取空间成员列表
         */
        async fetchSpaceMembers(id: number) {
            try {
                this.loading = true
                const members = await spaceApi.getMembers(id)
                this.spaceMembers = members
                return members
            } finally {
                this.loading = false
            }
        },

        /**
         * 修改成员角色
         */
        async updateMemberRole(spaceId: number, userId: number, payload: any) {
            await spaceApi.updateMemberRole(spaceId, userId, payload)
            // 重新获取成员列表
            await this.fetchSpaceMembers(spaceId)
        },

        /**
         * 踢出成员
         */
        async removeMember(spaceId: number, userId: number) {
            await spaceApi.removeMember(spaceId, userId)
            // 重新获取成员列表
            await this.fetchSpaceMembers(spaceId)
        },

        /**
         * 转让空间
         */
        async transferSpace(id: number, payload: any) {
            await spaceApi.transfer(id, payload)
            // 重新获取空间详情和成员列表
            await this.fetchSpaceById(id)
            await this.fetchSpaceMembers(id)
        },

        /**
         * 删除空间
         */
        async deleteSpace(id: number) {
            try {
                this.loading = true
                await spaceApi.delete(id)
                // 重新获取我的空间列表
                await this.fetchMySpaces()
                // 如果删除的是当前空间，清空当前空间
                if (this.currentSpace?.id === id) {
                    this.currentSpace = null
                }
            } finally {
                this.loading = false
            }
        },

        /**
         * 清空状态
         */
        clearState() {
            this.currentSpace = null
            this.mySpaces = null
            this.spaceMembers = null
        },
    },
})




